<template>
  <div class="context_all">
    <div class="context_title">
      <span class="title1">{{ $t("chatGpt.context") }}</span>
      <span class="title2">{{ $t("chatGpt.contextSub") }}</span>
    </div>
    <!-- 分类 -->
    <div class="context_classify">
      <div class="classify_box">
        <span class="classify_name" :class="[classify_name_active.length == 0 ? 'classify_name_active' : '']"
          @click="classifyNameClick('')">
          {{ $t("chatGpt.allBtn") }}
        </span>
        <span class="classify_name" :class="[classify_name_active.includes(v.value) ? 'classify_name_active' : '']"
          v-for="(v, i) in helper_type" :key="v.value" @click="classifyNameClick(v)">{{ v.label }}</span>
      </div>
      <div class="classify_search">
        <img src="../../../../assets/images/chatgpt/search1.png" style="width:22px;" @click="openInput" />
        <el-input v-show="isSearchInput" v-model="searchKeyword" :placeholder="$t('chatGpt.searchPlaceholder')"
          class="input" :class="[isSearchInput ? 'search_input_true' : 'search_input_false']"
          @keydown.enter.native="apiHelperGetAll"></el-input>
      </div>
    </div>
    <!-- 查询的提示词 -->
    <div class="scoll_y" v-loading="tableLoading">
      <div class="call_word_all">
        <div class="callWord_all_item" v-for="(item, index) in dataList" :key="index" @click="skipChatpage(item)">
          <div class="title_text">
            <div class="title_text_bg"
              :style="{ backgroundColor: `rgba(${item.bgColor},.15)`, color: `rgb(${item.fontColor})` }">
              {{ item.titleText }}
            </div>
          </div>
          <div class="title">{{ item.titleName }}</div>
          <div class="sub_title">
            {{ item.desc }}
          </div>
          <div class="enter_info">
            <div v-if="item.status == '未添加'" class="add" @click.stop="addOrdel(item)">
              <el-icon><CirclePlusFilled /></el-icon>
              <span> {{ $t("chatGpt.addOrdel1") }}</span>
            </div>
            <div v-else class="add_success" @click.stop="addOrdel(item)">
              <el-icon><CircleCheckFilled /></el-icon>
              <span> {{ $t("chatGpt.addOrdel2") }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="yu-context-all" lang="ts">
import chatAgentApi from "@/api/chatgpt/chatAgentApi";
import useUserStore from '@/store/modules/user';


const tableLoading = ref(false)
const classify_name_active = ref([])
const searchKeyword = ref("")
const dataList = ref([])
const isSearchInput = ref(false)
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { helper_type } = toRefs<any>(proxy?.useDict('helper_type'));
const userStore = useUserStore();

const emit = defineEmits(['skipChatpage','addOrdelClick'])
//  获取全部语境中心数据
const apiHelperGetAll = () => {
  tableLoading.value = true;
  chatAgentApi
    .apiHelperGetAll({
      keyword: searchKeyword.value,
      type: classify_name_active.value.join(","),
      userId: userStore.userId,
      // sort: "id,desc"
    })
    .then((res:any) => {
      dataList.value = res.data;
      tableLoading.value = false;
    })
    .catch((err:any) => {
      tableLoading.value = false;
    });
}
//选择语境中心分类
const classifyNameClick = (v: any) => {
  if (v == "") {
    classify_name_active.value = [];
  } else {
    if (classify_name_active.value.includes(v.value)) {
      const index = classify_name_active.value.indexOf(v.value);
      if (index > -1) {
        classify_name_active.value.splice(index, 1);
      }
    } else {
      classify_name_active.value.push(v.value);
    }
  }
  dataList.value = [];
  apiHelperGetAll();
}
//  添加或删除语境中心
const addOrdel = (item: any) => {
  // this.$parent.apiHelperAddHelper(item);
  emit("addOrdelClick",item)
}
// 跳转语境中心
const skipChatpage = (item: any) => {
  if (item.status == "已添加") {
    // this.$parent.contextClick(item);
    emit("skipChatpage",item)
  } else {
    addOrdel(item);
  }
}
//  打开搜索
const openInput = () => {
  isSearchInput.value = !isSearchInput.value;
}

onMounted(() => {
  apiHelperGetAll();
})

</script>


<style scoped lang="scss">
@mixin textOverflow($clamp) {
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: $clamp; //元素几行显示
  -webkit-box-orient: vertical;
}

.context_all {
  width: 100%;
  height: calc(100vh - 110px);
}

.context_title {
  margin-bottom: 20px;
  padding: 0 20px;

  .title1 {
    font-size: 18px;
    font-weight: 700;
    color: #4c75f6;
  }

  .title2 {
    font-size: 11px;
    font-weight: 300;
    color: #9295bf;
    margin-left: 13px;
    margin-bottom: 20px;
  }
}

.context_classify {
  display: flex;
  margin-bottom: 20px;
  padding: 0 20px;

  .classify_box {
    width: calc(100% - 26px);

    .classify_name {
      display: inline-block;
      // width: 61px;
      // height: 24px;
      padding: 6px 11px;
      // line-height: 22px;
      background: #f9fbfc;
      border-radius: 12px;
      font-size: 12px;
      color: #43436b;
      text-align: center;
      margin-right: 8px;
      margin-bottom: 11px;
      cursor: pointer;
    }

    .classify_name_active {
      color: #6178ff;
    }

    .classify_name:hover {
      @extend .classify_name_active;
    }
  }

  .classify_search {
    width: 24px;
    height: 24px;
    background: #ffffff;
    border-radius: 12px;
    font-size: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: relative;

    img {
      position: relative;
      z-index: 10;
    }

    .input {
      position: absolute;
      z-index: 2;
      right: 28px;
      top: -4px;
      width: 200px;

      &:deep(.el-input__wrapper) {
        box-shadow: none;
        height: 24px;
        line-height: 24px;
        width: 100%;
      }
    }
  }

  .search_input_true {
    animation: search_input_true 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
  }

  .search_input_false {
    animation: search_input_false 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
  }
}

.scoll_y {
  height: calc(100% - 150px);
  overflow-y: scroll;
}

.call_word_all {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  flex-wrap: wrap;

  .callWord_all_item {
    width: 256px;
    position: relative;
    padding: 7px 14px;
    background-color: #ffffff;
    border-radius: 15px;
    margin: 30px 0 0;
    color: #979ba4;
    font-size: 13px;
    border: 3px solid #ffffff;
    cursor: pointer;
    transition: box-shadow 0.3s;
    transition: border 0.3s;

    .title_text {
      position: absolute;
      z-index: 10;
      top: -17px;
      left: 23px;
      width: 34px;
      height: 34px;
      background-color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 5px;
      transition: box-shadow 0.5s;
      transition: transform 0.5s;
    }

    .title_text_bg {
      background-color: #fffaab;
      width: 27px;
      height: 27px;
      border-radius: 4px;
      text-align: center;
      line-height: 27px;
      font-size: 18px;
    }

    .title {
      font-size: 15px;
      font-weight: 500;
      color: #43436b;
      margin-left: 50px;
      margin-bottom: 4px;
    }

    .sub_title {
      height: 36px;
      font-size: 12px;
      font-weight: 500;
      line-height: 18px;
      @include textOverflow(2);
    }

    .enter_info {
      margin-top: 6px;
      font-size: 14px;
      font-weight: 400;
      text-align: right;

      .add {
        color: #43436b;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        i {
          color: #88aafe;
          font-size: 18px;
          margin-right: 5px;
        }
      }

      .add_success {
        color: #96b98a;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        i {
          font-size: 18px;
          margin-right: 5px;
        }
      }
    }
  }

  .callWord_all_item:hover {
    //  box-shadow: 0px 8px 22px 0px rgba(0, 0, 0, 0.16);
    border: 3px solid #9bf0db;
  }

  .callWord_all_item:hover .title_text {
    box-shadow: 0px 8px 22px 0px rgba(0, 0, 0, 0.16);
    transform: scale(1.08);
  }
}

@keyframes search_input_true {
  0% {
    width: 0px;
    display: none;
  }

  100% {
    width: 200px;
    display: block;
  }
}

@keyframes search_input_false {
  0% {
    width: 200px;
    display: block;
    right: 24px;
  }

  70% {
    display: none;
  }

  100% {
    width: 0px;
    right: 48px;
  }
}
</style>
